#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <string.h>

const int maxevents = 3000;

// 不单单只是使用简单的fd，而是使用结构体封装更多的数据进去。
typedef struct sockinfo
{
    int fd;
    struct sockaddr_in addr;
} SockInfo;

int main(int argc, char const *argv[])
{
    if(argc < 2)
    {
        printf("eg a.out port\n");
        exit(1);
    }
    int port = atoi(argv[1]);
    int lfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in serv;
    memset(&serv, 0, sizeof(serv));
    serv.sin_family = AF_INET;
    serv.sin_port = htons(port);
    serv.sin_addr.s_addr = htonl(INADDR_ANY);
    // 绑定
    bind(lfd, (const struct sockaddr *)&serv, sizeof(serv));
    // 监听；
    listen(lfd, 128);

    struct sockaddr_in client;
    socklen_t len = sizeof(client);

    int epfd = epoll_create(maxevents);
    struct epoll_event *events = (struct epoll_event *)malloc(sizeof(struct epoll_event)*maxevents);
    struct epoll_event ev;

    ev.events = EPOLLIN;
    SockInfo *temp = (SockInfo *)malloc(sizeof(SockInfo));
    temp->fd = lfd;
    temp->addr = serv;
    ev.data.ptr = (void *)temp;
    epoll_ctl(epfd, EPOLL_CTL_ADD, lfd, &ev);
    while(1)
    {
        int ret = epoll_wait(epfd, events, maxevents, -1);
        for(int i=0; i<ret; ++i)
        {
            // 有新的连接请求
            SockInfo *accept_info = (SockInfo *)(events+i)->data.ptr;
            int fd = accept_info->fd;
            if(fd == lfd)
            {
                int cfd = accept(lfd, (struct sockaddr *)&client, &len);
                if (cfd == -1)
                {
                    printf("客户端链接失败\n");
                    exit(1);
                }
                char ip[64];
                printf("client IP: %s, port: %d\n", inet_ntop(AF_INET, &client.sin_addr.s_addr, ip, sizeof(ip)), ntohs(client.sin_port));
                SockInfo *temp = (SockInfo *)malloc(sizeof(SockInfo));
                temp->fd = cfd;
                temp->addr = client;
                struct epoll_event ev;
                ev.events = EPOLLIN;
                ev.data.ptr = (void *)temp;
                epoll_ctl(epfd, EPOLL_CTL_ADD, cfd, &ev);


            }
            // 连接客户端发来了数据
            else
            {
                if(!((events+i)->events & EPOLLIN))
                {
                    continue;
                }
                char buf[1024] = {0};
                int len = recv(fd, buf, sizeof(buf), 0);
                if(len == -1)
                {
                    printf("recv error\n");
                    exit(1);
                }
                else if(len == 0)
                {
                    printf("客户端断开了连接\n");
                    epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
                    close(fd);
                    // 释放资源
                    free((events+i)->data.ptr);
                }
                else
                {
                    char ip[64];
                    SockInfo *info = (SockInfo *)(events+i)->data.ptr;
                    printf("client IP: %s, port: %d    ", inet_ntop(AF_INET, &info->addr.sin_addr.s_addr, ip, sizeof(ip)), ntohs(info->addr.sin_port));
                    printf("recv buf: %s\n", buf);
                    send(fd, buf, len, 0);
                }
            }
        }
    }
    free(temp);
    free(events);
    close(lfd);
    return 0;
}
